/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "iccreader_srv_client.h"
#include "string_ex.h"
#include "datetime_ex.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "if_system_ability_manager.h"
#include "system_ability_definition.h"
#include "iccreader_errors.h"
#include "iccreader_common.h"
#include "iccreader_hilog.h"

namespace SwanLink {
namespace Finance {
IccreaderSrvClient::IccreaderSrvClient() {}
IccreaderSrvClient::~IccreaderSrvClient() {}

ErrCode IccreaderSrvClient::Connect()
{
    std::lock_guard<std::mutex> lock(mutex_);
    if (proxy_ != nullptr) {
        return ERR_OK;
    }
    sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    if (sysMgr == nullptr) {
        ICCREADER_HILOGE(ICCREADER_SRV, "failed to GetSystemAbilityManager,%{public}d",FINANCE_ICCREADER_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }
    sptr<IRemoteObject> remoteObject_ = sysMgr->CheckSystemAbility(FINANCE_ICCREADER_SERVICE_ID);
    if (remoteObject_ == nullptr) {
        ICCREADER_HILOGE(ICCREADER_SRV, "failed to GetSystemAbility by %{public}d",FINANCE_ICCREADER_SERVICE_ID);
        return E_GET_SYSTEM_ABILITY_MANAGER_FAILED;
    }

    deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new IccreaderSrvDeathRecipient());
    if (deathRecipient_ == nullptr) {
        ICCREADER_HILOGE(ICCREADER_SRV, "failed to Create DeathRecipinent,%{public}d",FINANCE_ICCREADER_SERVICE_ID);
        return ERR_NO_MEMORY;
    }
    if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
        ICCREADER_HILOGW(ICCREADER_SRV, "Add death recipient to failed,%{public}d",FINANCE_ICCREADER_SERVICE_ID);
        return E_ADD_DEATH_RECIPIENT_FAILED;
    }

    proxy_ = iface_cast<IIccreaderSrv>(remoteObject_);
    return ERR_OK;
}

void IccreaderSrvClient::ResetProxy(const wptr<IRemoteObject>& remote)
{
    std::lock_guard<std::mutex> lock(mutex_);
    RETURN_IF(proxy_ == nullptr);
    auto serviceRemote = proxy_->AsObject();
    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
        serviceRemote->RemoveDeathRecipient(deathRecipient_);
        proxy_ = nullptr;
    }
}

void IccreaderSrvClient::IccreaderSrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
{
    if (remote == nullptr) {
        return;
    }
    IccreaderSrvClient::GetInstance().ResetProxy(remote);
}

int IccreaderSrvClient::GetDeviceInfo(int fd,DeviceInfo &devInfo)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "GetDeviceInfo failed");
        return -1;
    }
    return proxy_->GetDeviceInfo(fd,devInfo);
}

int IccreaderSrvClient::IccIOCtrl(int fd,IOType type,int value)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "IccIOCtrl failed");
        return -1;
    }
    return proxy_->IccIOCtrl(fd,type,value);
}

int IccreaderSrvClient::ApduCommand(int fd,IOType type,int len,uint8_t cmd[],uint8_t cmd_out[])
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "ApduCommand failed");
        return -1;
    }
    return proxy_->ApduCommand(fd,type,len,cmd,cmd_out);
}

int IccreaderSrvClient::CardSearch(int fd,int timeout,SearchResult *result)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "CardSearch failed");
        return -1;
    }
    return proxy_->CardSearch(fd,timeout,result);
}

int IccreaderSrvClient::CardStopSearch(int fd)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "CardStopSearch failed");
        return -1;
    }
    return proxy_->CardStopSearch(fd);
}

int IccreaderSrvClient::RfCardActive(int fd,RfCardInfo &cardInfo)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "RfCardActive failed");
        return -1;
    }
    return proxy_->RfCardActive(fd,cardInfo);
}

int IccreaderSrvClient::RfCardDormancy(int fd)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "RfCardDormancy failed");
        return -1;
    }
    return proxy_->RfCardDormancy(fd);
}

int IccreaderSrvClient::ScanData(int fd,const DeviceConfig& config,ResultData &result)
{
    int ret = Connect();
    if (ret != ERR_OK) {
        ICCREADER_HILOGE(ICCREADER_JS_NAPI, "ScanData failed");
        return -1;
    }
    return proxy_->ScanData(fd,config,result);
}



}  // namespace Finance
}  // namespace SwanLink
